package com.sllp.C71S3TsllpShop.web;

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.sllp.C71S3TsllpShop.bean.AlipayConfig;

@Controller
public class AlipayConfig1 {
//	// 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号，alipayrisk10@alipay.com账号
//	public static String app_id = "2016101500689736";
//	// 商户私钥，您的PKCS8格式RSA2私钥
//	public static String merchant_private_key="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKbzu3A2LAVsriBGb0APWqWyhABAQqfVBkuvagmi1ptwVPeMYu0cY/jXqhaG/29SkRdow+H4MfIu2/VpYWhv8+Jnq8n7XomKAohTeWserall8sdssoKTVJ0+HZ5pXf6wzUw52bNBdro9vbcYFzcS+IDtvwbxREC8Lp0soQ9RNB0bAgMBAAECgYAbambbVGoCMgieEDLwqBTQpPH8QJExkk/IWfPlxTHMsLOjOxB7s2/PS0sJC/4iP8LHv1J7ZxB8nqlMMZPnXIsF3O5NCmBomG9je5rtbHCX12bPbFYYftdGN9UZuN+fUIDCccLLDgrGa/CPIuX5MaaGfTRTSnvQcZsohIDL8u6JgQJBAM9y69A3Xfa2gEeqkLghxTYhHdPcUkpHoIcPSCtYGtIicHTaHQJT1HF2ojEZI6kGNBH46rMW9CDZcjz/ehIW1vsCQQDOBnyfxZ5mn+hin00qgbQi6SUS7NB+uHgZB1qeqbu2s+w30IQZy00pNyqDoUFVHYra2b83A7JiD+VXmTL2pXhhAkEAmynO/ddb3dvViUUnp8ICmyEcCUoi9tIqXLrudRUaQpBl4tD9n68KGK/uUqEGDjfInKS9D6UchrF+A+fm6z7QHwJBAL9Cdl88bhiHSoyDpndXgYsHNUukXMnHJnbVxOKWX5T04s3N3VS0zyT7+ZPJF4WUr1sL2d+QpwEyEI6Jwi4G3wECQEsoT8bARI8B8PTiw0utPsVwslJ/3k5iHl57i+wWxyC+4S9ZodYtSovPeFgaOsLUiKNj/P+9Z4jM3MWmmzs3J2I=";
//	
//	//支付宝公钥
//	public static String alipay_public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB";
//
//	// 签名方式
//	public static String sign_type = "RSA";
//	
//	// 字符编码格式
//	public static String charset = "utf-8";
//	
	
	@GetMapping("AlipayConfig1.do")
	public void pay(HttpServletRequest request ,HttpServletResponse response,String orderId,BigDecimal money) throws IOException, ServletException, AlipayApiException{

	      /*开始订单业务处理*/

	        //开始alipay处理
	        //获得初始化的AlipayClient
	        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

	        //设置请求参数
	        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
	        alipayRequest.setReturnUrl(AlipayConfig.return_url);
	        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

	        //商户订单号，商户网站订单系统中唯一订单号，必填
	        String out_trade_no = new String(orderId.getBytes("ISO-8859-1"),"UTF-8");
	        //付款金额，必填
	        String total_amount = new String(money.toString().getBytes("ISO-8859-1"),"UTF-8");
	        //订单名称，必填
	        String subject = new String("尤洪商城".toString().getBytes("ISO-8859-1"),"UTF-8");
	        //商品描述，可空654334
	        String body = new String("尤洪商城".toString().getBytes("ISO-8859-1"),"UTF-8");


	        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
	                + "\"total_amount\":\""+ total_amount +"\"," 
	                + "\"subject\":\""+ subject +"\"," 
	                + "\"body\":\""+ body +"\"," 
	                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
	        String form ="";
	        form = alipayClient.pageExecute(alipayRequest).getBody();
	        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
	        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
	        //        + "\"total_amount\":\""+ total_amount +"\"," 
	        //        + "\"subject\":\""+ subject +"\"," 
	        //        + "\"body\":\""+ body +"\"," 
	        //        + "\"timeout_express\":\"10m\"," 
	        //        + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
	        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

	        //请求
	        System.out.println("111111111111111111111111111");
	    	String result = alipayClient.pageExecute(alipayRequest).getBody();
	    	System.out.println("22222222222222222222222222222222222222");
	    	System.out.println("333333333333333333"+result);
	    	//输出
	    	response.setContentType("text/html;charset=utf-8");
			response.getWriter().println(form);//直接将完整的表单html输出到页面
			response.getWriter().close();

	    }


}
